package com.ts_xiaoa.ts_ui.dialog

import android.os.Bundle
import androidx.annotation.ColorInt
import androidx.fragment.app.FragmentManager
import com.ts_xiaoa.ts_recycler_view.BaseViewHolder
import com.ts_xiaoa.ts_recycler_view.databinding.TsRvWheelStringBinding
import com.ts_xiaoa.ts_recycler_view.widget.wheel.WheelStringAdapter
import com.ts_xiaoa.ts_ui.R
import com.ts_xiaoa.ts_ui.base.BaseBottomDialogFragment
import com.ts_xiaoa.ts_ui.databinding.TsDialogSimpleWheelBinding

/**
 * create by ts_xiaoA on 2020-08-01 14:16
 * email：443502578@qq.com
 * desc：简单的wheel dialog
 */
class TsSimpleWheelDialog<T> private constructor(
    //数据
    private val data: MutableList<T>,
    private val autoCancel: Boolean = true,
    itemBinder: ((holder: BaseViewHolder, itemBinding: TsRvWheelStringBinding, item: T) -> Unit)? = null,
    //选择完成后的监听回调
    private val listener: ((data: T) -> Unit)? = null
) : BaseBottomDialogFragment() {

    val binding by lazy { rootBinding as TsDialogSimpleWheelBinding }

    //界面title
    private var dialogTitle: CharSequence? = ""

    //左边按钮的文字
    private var leftText: CharSequence? = "取消"

    //右边按钮的文字
    private var rightText: CharSequence? = "完成"

    //指示器颜色 中间的那个颜色
    private var indicatorColor: Int = 0

    //指示器透明度
    private var indicatorAlpha = 1f

    //显示adapter
    private var dataAdapter: WheelStringAdapter<T>? = null


    init {
        dataAdapter = WheelStringAdapter(data = data, itemBinder = itemBinder)
    }

    override fun getLayoutId(): Int {
        return R.layout.ts_dialog_simple_wheel
    }


    override fun initView(savedInstanceState: Bundle?) {
        binding.tvTitle.text = dialogTitle
        binding.tvCancel.text = leftText ?: "取消"
        binding.tvFinish.text = rightText ?: "完成"
        binding.viewIndicator.setBackgroundColor(indicatorColor)
        binding.viewIndicator.alpha = indicatorAlpha
        binding.wrData.adapter = dataAdapter
    }


    override fun initEvent(savedInstanceState: Bundle?) {
        binding.tvCancel.setOnClickListener { dismiss() }
        binding.tvFinish.setOnClickListener {
            listener?.invoke(data[binding.wrData.selectPosition])
            if (autoCancel) dismiss()
        }
    }

    class Builder<T> constructor(
        //数据
        private val data: MutableList<T>
    ) {
        //界面title
        private var dialogTitle: CharSequence? = ""

        //左边按钮的文字
        private var leftText: CharSequence? = "取消"

        //右边按钮的文字
        private var rightText: CharSequence? = "完成"

        //指示器颜色 中间的那个颜色
        private var indicatorColor: Int = 0

        //指示器透明度
        private var indicatorAlpha = 1f

        //点击后弹框自动消失
        private var autoCancel: Boolean = true

        //数据绑定
        private var itemBinder: ((holder: BaseViewHolder, itemBinding: TsRvWheelStringBinding, item: T) -> Unit)? =
            null

        //选择完成后的监听回调
        private var listener: ((data: T) -> Unit)? = null

        //左边按钮文字
        fun setTitleText(titleText: CharSequence? = null): Builder<T> {
            this.dialogTitle = titleText
            return this
        }

        //左边按钮文字
        fun setLeftText(leftText: CharSequence? = null): Builder<T> {
            this.leftText = leftText
            return this
        }

        //右边按钮文字
        fun setRightText(rightText: CharSequence? = null): Builder<T> {
            this.rightText = rightText
            return this
        }

        //指示器颜色、透明度
        fun setIndicator(@ColorInt indicatorColor: Int, indicatorAlpha: Float = 0.1f): Builder<T> {
            this.indicatorColor = indicatorColor
            this.indicatorAlpha = indicatorAlpha
            return this
        }

        //是否默认取消
        fun setAutoCancel(autoCancel: Boolean): Builder<T> {
            this.autoCancel = autoCancel
            return this
        }

        //设置数据绑定
        fun setItemBinder(itemBinder: ((holder: BaseViewHolder, itemBinding: TsRvWheelStringBinding, item: T) -> Unit)? = null): Builder<T> {
            this.itemBinder = itemBinder
            return this
        }

        //设置回调监听
        fun setListener(listener: ((data: T) -> Unit)? = null): Builder<T> {
            this.listener = listener
            return this
        }

        //构建对象
        fun builder(): TsSimpleWheelDialog<T> {
            val dialog = TsSimpleWheelDialog(data, autoCancel, itemBinder, listener)
            dialog.leftText = this.leftText
            dialog.rightText = this.rightText
            dialog.dialogTitle = this.dialogTitle
            dialog.indicatorAlpha = this.indicatorAlpha
            dialog.indicatorColor = this.indicatorColor
            return dialog
        }

        //直接显示
        fun show(fragmentManager: FragmentManager) {
            this.builder().show(fragmentManager)
        }

    }
}